﻿using System;
using System.IO;

namespace MSPE.Public.FOSS.AutoStatusPlus.Common
{
    internal class AppLog
    {
        #region Public Properties
        /// <summary>
        /// Stream writer
        /// </summary>
        public StreamWriter SwLog { get; private set; }

        /// <summary>
        /// File name
        /// </summary>
        public string FileName { get; private set; }
        #endregion // Public Properties

        #region Constructors
        /// <summary>
        /// Create log file
        /// </summary>
        /// <param name="prefixName">Prefix name</param>
        /// <param name="logFilePath">Folder location</param>
        /// <param name="newLogFileEveryHour">Create a log file every hour</param>
        public AppLog(string prefixName, string logFilePath, bool newLogFileEveryHour)
        {
            FileName = string.Format("{0}AutoStatusPlus_TimesheetEvent_{1}.log", prefixName, Utility.FormatTimeLog(true));

            if (!logFilePath.EndsWith(@"\")) logFilePath += @"\";
            string _path = logFilePath + FileName;
            try
            {
                if (!newLogFileEveryHour)
                    SwLog = new StreamWriter(_path);
                else
                {
                    // Check to see if file exists
                    if (File.Exists(_path))
                        SwLog = new StreamWriter(_path, true);
                    else
                        SwLog = new StreamWriter(_path);
                }
            }
            catch (Exception)
            {
                System.Threading.Thread.Sleep(1000);
                SwLog = new StreamWriter(Environment.CurrentDirectory + "\\" + string.Format("{0}AutoStatusPlus_TimesheetEvent_{1}.log", prefixName, Guid.NewGuid()));
            }
        }
        #endregion // Constructors

        #region Methods
        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        public void WriteLine(string toWrite)
        {
            WriteLine(toWrite, true);
        }

        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="toWrite">entry to write</param>
        /// <param name="dateTime">write date time?</param>
        public void WriteLine(string toWrite, bool dateTime)
        {
            if (dateTime)
                SwLog.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToLongTimeString(), toWrite));
            else
                SwLog.WriteLine(toWrite);
        }

        /// <summary>
        /// Write entry to log file
        /// </summary>
        /// <param name="sw">stream writer</param>
        /// <param name="toWrite">entry to write</param>
        public static void WriteLine(StreamWriter sw, string toWrite)
        {
            sw.WriteLine(DateTime.Now.ToLongTimeString() + " : " + toWrite);
        }

        /// <summary>
        /// Close stream writer
        /// </summary>
        public void Close()
        {
            SwLog.Close();
        }
        #endregion // Methods
    }
}
